package com.cts.controller.admin;

import com.cts.annotation.OperationLogger;
import com.cts.entity.Category;
import com.cts.model.dto.CategoryDTO;
import com.cts.model.vo.PageResultVO;
import com.cts.model.vo.Result;
import com.cts.service.CategoryService;
import com.cts.util.DictUtils;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @projectName: CBlog
 * @package: com.cts.controller.admin
 * @className: CategoryAdminController
 * @author: chentongsheng
 * @description: TODO
 * @date: 2023/2/23 20:59
 * @version: 1.0
 */
@RestController
@RequestMapping("/admin")
public class CategoryAdminController {

    @Autowired
    CategoryService categoryService;

    @OperationLogger("新增分类")
    @ApiOperation("新增分类")
    @PostMapping("/category")
    public Result addCategory(@RequestBody Category category){
        String categoryName = category.getCategoryName();
        if(StringUtils.isNullOrEmpty(categoryName)) {
            return Result.error("分类不能为空");
        }
        categoryService.save(category);
        return Result.ok("新增成功");
    }

    @OperationLogger("更新分类")
    @ApiOperation("更新分类")
    @PostMapping("/updateCategory")
    public Result updateCategory(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.ok("更新成功");
    }

    @OperationLogger("删除分类")
    @ApiOperation("删除分类")
    @PostMapping("/deleteCategory")
    public Result deleteCategory(@RequestBody Long id){
        categoryService.removeById(id);
        return Result.ok("删除成功");
    }

    @OperationLogger("查询分类列表")
    @ApiOperation("查询分类列表")
    @PostMapping("/getCategoryList")
    public Result getCategoryList(@RequestBody CategoryDTO categoryDTO){
        PageResultVO<Category> pageResult = categoryService.getCateGoryList(categoryDTO);
        return Result.ok("请求成功", pageResult);
    }

    @OperationLogger("查询分类字典")
    @ApiOperation("查询分类字典")
    @GetMapping("/getCateGoryDict")
    public Result getCateGoryDict(){
        List<Category> cateGoryDict = categoryService.list();
        return Result.ok("请求成功", cateGoryDict);
    }

}
